//
//  HomepageMoreSubjectVC.m
//  Tour
//
//  Created by hesiyao on 16/3/9.
//  Copyright © 2016年 张中烨. All rights reserved.
//

#import "HomepageMoreSubjectVC.h"

@interface HomepageMoreSubjectVC () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, retain) BaseTableView *tableView;
@property (nonatomic, assign) NSInteger page;/**< 加载变化值 */
@end

@implementation HomepageMoreSubjectVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"专题";
    //动态值初始化
    self.page = 1;
    //数据处理
    [self dataHandler];
    //初始化视图
    [self createSubviews];
    //刷新
    [self refresh];
}

- (void)dataHandler
{
    [AFNModel GETWithHandler:@"http://open.qyer.com/qyer/special/topic/special_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88250892208679&lon=121.5399580135169&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone7%2C2&track_deviceid=0C79B4F1-2C09-4E9A-805E-05836431AB22&track_os=ios%25209.2&v=1" completion:^(id result) {
        self.dataArr = [HomeGetMoreModel modelHanderWithArray:result[@"data"]];
//        NSLog(@"%@",_dataArr);
        // 刷新
        [_tableView reloadData];
    }];
}

- (void)createSubviews
{
    // tableView 初始化
    self.tableView = [[BaseTableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    // 设置代理人
    _tableView.delegate =self;
    _tableView.dataSource = self;
    
    // 注册重用池
    [_tableView registerClass:[HomepageMoreSubjectCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
//  设置cell 内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomepageMoreSubjectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    HomeGetMoreModel *gm = _dataArr[indexPath.row];
    [cell.photo sd_setImageWithURL:[NSURL URLWithString:gm.photo]placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    [cell NightWithType:UIViewColorTypeNormal];
    return cell;
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SCREEN_HEIGHT / 3;
}
//点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消tableview 选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // 隐藏tabbar
    self.hidesBottomBarWhenPushed = YES;
    // 跳转webview
    HomeCarouseWebVC *webview = [[HomeCarouseWebVC alloc]init];
    // 获取每个cell 的webView网址
    HomeGetMoreModel *model = _dataArr[indexPath.row];
    webview.urlstr = model.url;
    [self.navigationController pushViewController:webview animated:YES];
    self.hidesBottomBarWhenPushed = NO;

}

//上拉刷新
- (void)refresh
{
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // page 值 自加1
        _page++;
        //  下拉刷新数据处理
        [self refreshData];
        
    }];
}

- (void)refreshData
{
    NSString *str =[NSString stringWithFormat:@"http://open.qyer.com/qyer/special/topic/special_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&oauth_token=89dd34fbffaaa72503b4baa7a9d02812&page=%ld",(long)_page];
    NSString *url = @"&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_os=ios%25207.1.2&track_user_id=7328550&v=1";
    [AFNModel GETWithHandler:[NSString stringWithFormat:@"%@%@",str,url] completion:^(id result) {
        // 成功请求到数据
        NSArray *dataArr = result[@"data"];
        for (NSDictionary *dic in dataArr) {
            //  使用便利构造器
            HomeGetMoreModel *g = [HomeGetMoreModel modelWithDic:dic];
//            NSLog(@"~~%@",g);
            // 逐个将对象添加到数组中
            [self.dataArr addObject:g];
//            NSLog(@"!!%@",_dataArr);
        }
      //不能直接使用 BaseModel 中 创建的方法 要用便利构造器 逐个将对象添加 到数组中
        [_tableView reloadData];
        // 结束刷新
        [_tableView.mj_footer endRefreshing];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
